<template>
  <view class="content">
    <view class="content-item">
      <!-- 商品左侧图片区域 -->
      <view class="content-item-left">
        <!-- 勾选状态 -->
        <radio :checked="item.goods_state" color="#C00000" v-if="isShowRadio" @tap="radioClickHandler"></radio>
        <image :src="item.goods_small_logo || defaultPic"></image>
      </view>
      <!-- 商品右侧信息区域 -->
      <view class="content-item-right">
        <!-- 标题 -->
        <view class="title">
          {{item.goods_name}}
        </view>
        <!-- 价格 -->
        <view class="info">
          <view class="price">
            ￥{{item.goods_price | tofixed}}
          </view>
          <!-- 数量 -->
          <uni-number-box :min="1" :value="item.goods_count" @change="changeNumber" v-if="isShowNumber"></uni-number-box>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:"goodsItem",
    data() {
      return {
        // 默认的空图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    filters: {
      // 把价格保留两位小数
      tofixed(num) {
        return Number(num).toFixed(2)
      }
    },
    props: {
      item: {
        type: Object,
        default:{}
      },
      // 是否显示radio
      isShowRadio: {
        type: Boolean,
        default: false
      },
      // 是否显示数量
      isShowNumber: {
        type: Boolean,
        default: false
      }
    },
    methods: {
      // 向外部发送一个radio-change事件，将该商品的id和state发送出去，让其在外面改变其状态
      radioClickHandler() {
        this.$emit('radio-change', {
          goods_id: this.item.goods_id,
          goods_state: !this.item.goods_state
        })
      },
      // 改变该商品数量
      changeNumber(value) {
        this.$emit('count-change', {
          goods_id: this.item.goods_id,
          goods_count: value
        })
      }
    }
  }
</script>

<style lang="scss">
.content {
  &-item {
    // width: 750rpx;
    // box-sizing: border-box;
    display: flex;
    padding: 20rpx 10rpx;
    border-bottom: 2rpx solid #f0f0f0;
    &-left {
      margin-right: 10rxp;
      display: flex;
      justify-content: space-between;
      align-items: center;
      image {
        width: 200rpx;
        height: 200rpx;
        // display: block;
      }
    }
    &-right {
      display: flex;
      flex: 1;
      flex-direction: column;
      justify-content: space-around;
      
      .title {
        font-size: 26rpx;
      }
      
      .info {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
      
      .price {
        font-size: 32rpx;
        color: #c00000;
      }
    }
  }
}
</style>
